package com.itcast.flink.connectors.kafka.serial;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.springframework.util.StringUtils;

/**
 * <p>Description: </p>
 *
 * @author
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 * @date
 */
public class KafkaSinkApplication {
    
    public static void main(String[] args) throws Exception {
        
        // 1. 创建运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 读取Socket数据源
        DataStreamSource<String> socketStr = env.socketTextStream("192.168.23.128", 9911, "\n");
        // 3. 转换处理流数据
        SingleOutputStreamOperator<AccessLog> outputStream = socketStr.map(new MapFunction<String, AccessLog>() {
            @Override
            public AccessLog map(String value) throws Exception {
                System.out.println(value);
                // 将数据组装为对象
                AccessLog log = new AccessLog();
                if (StringUtils.isEmpty(value)) {
                    log.setIp("10.10.20.107");
                    log.setTime("1603847905848");
                    log.setType("view");
                    log.setApi("10006");
                    return log;
                }
                // 根据分隔符解析数据
                String[] arrValue = value.split("\t");
                log.setNum(1);
                for (int i = 0; i < arrValue.length; i++) {
                    if (i == 0) {
                        log.setIp(arrValue[i]);
                    } else if (i == 1) {
                        log.setTime(arrValue[i]);
                    } else if (i == 2) {
                        log.setType(arrValue[i]);
                    } else if (i == 3) {
                        log.setApi(arrValue[i]);
                    }
                }
                
                return log;
            }
        });
        
        // 3. Kakfa的生产者配置
        FlinkKafkaProducer kafkaProducer = new FlinkKafkaProducer(
                "192.168.23.140:9092", // kafka broker 连接列表信息
                "flink-serial",  // kafka 的连接topic
                new CustomSerialSchema() // 自定义序列化实现
        );
        
        // 4. 添加kafka的写入器
        outputStream.addSink(kafkaProducer);
        socketStr.print().setParallelism(1);
        
        // 5. 执行任务
        env.execute("job");
    }
}
